<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\System\UniversityCancelCertifyAction;
use App\Admin\Actions\Grid\System\UniversityCanCertifyAction;
use App\Admin\Repositories\WxUniversity;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\Settings;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class WxUniversityController extends AdminController
{
    public function universities(Request $request)
    {
        $q = $request->get('q');
        return \App\Models\WxUniversity::where('name', 'like', "%$q%")->orWhere('sid', $q)->orWhere('sid', '-1')->paginate(null, ['sid as id', 'name as text']);
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxUniversity(), function (Grid $grid) {
            global $__MINI_GLOBAL_TENANT_ID__;
            $grid->model()->orderBy('order', 'asc');
            if($__MINI_GLOBAL_TENANT_ID__ > 0){
                $grid->model()->whereIn('sid', Settings::get('tenant_'.$__MINI_GLOBAL_TENANT_ID__.'_with_student_certify', []));
            }
            $grid->column('sid')->sortable();
            $grid->quickSearch(['sid', 'name','charge','location'])->placeholder('搜索学校id，名字，主管部门，所在地...')->width(50);
            $grid->column('name')->editable();
//            $grid->column('order');
            $grid->column('charge');
            $grid->column('location');
            $grid->column('email_suffix')->editable();
//            $grid->column('class_time')->limit(10);
            $grid->column('level')->using([1=>'本科',2=>'专科', 3=>'成人高等学校'])->label([
                1=>'green',
                2=>'pink',
                3=>'blue'
            ])->sortable();
            $grid->column('private')->using([0=>'',1=>'民办'])->label([
                0=>'',
                1=>'default',
            ])->sortable();
            $grid->column('is_certify')->switch();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('sid');
                $filter->equal('is_certify')->radio([0=>'不开放', 1=>'开放'])->default(0);
            });
//            $grid->disableEditButton();
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableViewButton();
            $grid->disableBatchDelete();

            $grid->batchActions(function ($batch) {
                $batch->add(new UniversityCanCertifyAction());
                $batch->add(new UniversityCancelCertifyAction());
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxUniversity(), function (Show $show) {
            $show->field('sid');
            $show->field('name');
            $show->field('order');
            $show->field('charge');
            $show->field('location');
            $show->field('email_suffix');
            $show->field('class_time');
            $show->field('sem_start_date_1');
            $show->field('sem_end_date_1');
            $show->field('sem_start_date_2');
            $show->field('sem_end_date_2');
            $show->field('level');
            $show->field('private');
            $show->field('is_certify');
            $show->disableEditButton();
            $show->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxUniversity(), function (Form $form) {

            $form->display('sid');
            $form->display('name');

            $modle = $form->model();
            $form->text('order')->default(0)->help('虽然一般是越大越靠前，但这个地方是越小越靠前');

            $form->text('email_suffix');

            $form->dateRange('sem_start_date_1', 'sem_end_date_1', '第一学期(下半年)');
            $form->dateRange('sem_start_date_2', 'sem_end_date_2', '第二学期(上半年)');

            $form->html(function () {
                return '<div style="background-color: #efe8d9; color: #dea550; padding: 10px;">提示: 必须是12节课，就是添加12个。</div>';
            }, '');

            $class_arr = [];
            $class_time_origin = [];
            if(is_string($modle->class_time)){
                $class_time_origin = json_decode($modle->class_time, true);
            }
            if(_empty_($class_time_origin)){
                $class_time_origin =  json_decode('[{"startTime":"08:00:00","endTime":"08:40:00"},{"startTime":"08:45:00","endTime":"09:25:00"},{"startTime":"09:40:00","endTime":"10:20:00"},{"startTime":"10:35:00","endTime":"11:15:00"},{"startTime":"11:20:00","endTime":"12:00:00"},{"startTime":"13:30:00","endTime":"14:10:00"},{"startTime":"14:15:00","endTime":"14:55:00"},{"startTime":"15:10:00","endTime":"15:50:00"},{"startTime":"15:55:00","endTime":"16:35:00"},{"startTime":"18:30:00","endTime":"19:10:00"},{"startTime":"19:20:00","endTime":"20:00:00"},{"startTime":"20:20:00","endTime":"20:50:00"}]', true);
            }
            if($class_time_origin){
                foreach ($class_time_origin as $class){
                    $class_arr[] = [
                        'startTime' => $class['startTime'],
                        'endTime' => $class['endTime'],
                    ];
                }
            }

            $form->table('class_time2', '课程时间', function (NestedForm $table) {
                $table->time('startTime', '开始时间');
                $table->time('endTime', '结束时间');
            })->saveAsJson()->value($class_arr);

            $form->switch('is_certify')->default(1);

            $form->text('class_time')->disable()->display(false);

            $form->saving(function (Form $form){
                $class_time_origin = [];
                if(is_array($form->class_time2)){
                    $class_time_origin = json_decode(AdminUtils::_table_data(array_values($form->class_time2)), true);
                }else if(is_string($form->class_time2)){
                    $class_time_origin = json_decode(AdminUtils::_table_data(array_values(json_decode($form->class_time2, true))));
                }

                $class_arr = [];
                if($class_time_origin){
                    foreach ($class_time_origin as $class){
                        $class_arr[] = [
                            'startTime' => $class['startTime'],
                            'endTime' => $class['endTime'],
                        ];
                    }
                }

                $form->class_time = $class_arr;

                $form->deleteInput(['class_time2']);
            });

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
